package cn.adair.oxframe.drawer;

import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatDialog;
import androidx.appcompat.app.AppCompatDialogFragment;

import cn.adair.oxframe.R;
import cn.adair.oxframe.views.OxStatusbar;


public abstract class DrawerBase extends AppCompatDialogFragment implements DrawerCallback {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(initLayout(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Log.e("DrawerBase", "------" + view.getMeasuredWidth());
        this.initView(view);
        this.initData(savedInstanceState);
        super.onViewCreated(view, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AppCompatDialog dialog = new AppCompatDialog(getContext(), R.style.Ox_Drawer);
        Window window = iHasTouchOutside(dialog);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window = this.iNavibarColor(window);
        }
        window = this.iFromIn(window);
        window.getDecorView().setPadding(0, 0, 0, 0);

        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        Log.e("DrawerBase", "width：" + width + "    height：" + height);
        OxStatusbar.transparencyBar(window);
        return dialog;
    }

    private DrawerFromIn fromIn; // 开向
    private int iNavibarColor; // 底部虚拟键背景颜色
    private boolean iHasNavibarColor = false; // 是否替换底部虚拟键背景颜色
    private boolean iHasTouchOutside = false; // 点击抽屉之外是否关闭抽屉


    public void setDrawerFromIn(DrawerFromIn fromIn) {
        Log.e("DrawerBase", "-------------抽屉方向");
        this.fromIn = fromIn;
    }

    public void setNavibarColor(int color) {
        this.iHasNavibarColor = true;
        this.iNavibarColor = color;
    }

    public void setHasTouchOutside(boolean has) {
        iHasTouchOutside = has;
    }

    /**
     * 配置方向
     * 目前左右抽屉 宽度问题还没解决
     *
     * @param window
     * @return
     */
    public Window iFromIn(Window window) {
        if (null == this.fromIn) this.fromIn = DrawerFromIn.BOTTOM; // 如果为null 设置默认方向
        WindowManager.LayoutParams mParams = window.getAttributes();
        switch (this.fromIn) {
            case TOP:
                mParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                mParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mParams.gravity = Gravity.TOP;
                window.setWindowAnimations(R.style.Ox_Drawer_FromIn_Top);
                break;
            case LEFT:
                mParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                mParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                mParams.gravity = Gravity.START;
                window.setWindowAnimations(R.style.Ox_Drawer_FromIn_Left);
                break;
            case RIGHT:
                mParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                mParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                mParams.gravity = Gravity.END;
                window.setWindowAnimations(R.style.Ox_Drawer_FromIn_Right);
                break;
            case BOTTOM:
                mParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                mParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mParams.gravity = Gravity.BOTTOM;
                window.setWindowAnimations(R.style.Ox_Drawer_FromIn_Bottom);
                break;
            case CENTER:
                mParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                mParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mParams.gravity = Gravity.CENTER;
                window.setWindowAnimations(R.style.Ox_Drawer_FromIn_Bottom);
                break;
        }
        window.setAttributes(mParams);
        return window;
    }

    /**
     * 配置Navibar 颜色
     *
     * @param window
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public Window iNavibarColor(Window window) {
        if (iHasNavibarColor) {
            window.setNavigationBarColor(iNavibarColor);
        }
        return window;
    }

    /**
     * 触摸抽屉之外是否关闭抽屉
     *
     * @param dialog
     * @return
     */
    public Window iHasTouchOutside(AppCompatDialog dialog) {
        dialog.setCanceledOnTouchOutside(iHasTouchOutside);
        dialog.setCancelable(iHasTouchOutside);
        return dialog.getWindow();
    }

}
